<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/2
 * Time: 下午7:25
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class PageController extends AdminBaseController{
    public function index(){//用于列表展示获取多条记录
        $list = D('Page')->pagelist(0,'id,title,link,name,sort','id');
        $this -> assign('list',$list['data']);
        $this -> display();
    }


    public function add(){
        $id = I('get.id/d',0);
        if(!empty($id)){ //用于编辑修改获取一条记录
            $list = M('Page')->field('id,title,link,name,keyword,description,content,sort')->where(['id'=>$id])->find();
            $this -> assign('list',$list);
            $this -> display();
        }else{ //添加单页
            $this -> display();
        }
    }

    //用于修改状态
    public function changeStatus(){
        if(IS_POST){
            $status = D('Page')->ChangeStatus(I('post.id/d',0));
            $this -> ajaxReturn($status);
        }else{
            $this -> ajaxReturn(['status'=>0,'msg'=>'非法操作!']);
        }
    }

    //删除数据库记录
    public function del(){
        if(IS_POST){
            $del = D('Page')->delList(I('post.id/d',0));
            if(!$del){
                $this -> ajaxReturn(['status'=>1,'msg'=>'删除失败！']);
            }
            $this -> ajaxReturn(['status'=>0,'msg'=>'删除成功！']);
        }else{
            $this -> ajaxReturn(['status'=>1,'msg'=>'非法操作！']);
        }
    }

    //添加保存，修改保存
    public function save(){
        if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            $title = I('post.title/s','');
            if(empty($title)){
                $this -> ajaxReturn(['status' => 1,'msg'=>'单页名称不能为空！']);
            }
            $name = I('post.name/s','');
            if(empty($name)){
                $this -> ajaxReturn(['status'=>1,'msg'=>'英文名称不能为空！']);
            }
            $sort = I('post.sort/d',0);
            $link = I('post.link/s','');
            $keyword = I('post.keyword/s','');
            $description = I('post.description/s','');
            $content = I('post.content/s','');
            $status = I('post.status/d',0);
//            $imgPath = '';
//            if(!$imgPath && !empty($_FILES['img']['type'])){
//                $imgPath = uploadFile($_FILES['img'],'Upload/page/');//上传文件
//            }
            $page = M('Page');
            if(empty($id)){
                $count = $page->where(['title'=>$title,'name'=>$name])->count();
                if($count > 0){
                    $this -> ajaxReturn(['status'=>1,'msg'=>'单页已存在！']);
                }
            }
            $data = [
                'id'=>$id,
                'title'=>$title,
                'name'=>$name,
                'link'=>$link,
                'keyword'=>$keyword,
                'description'=>$description,
                'content'=>$content,
                'status'=>$status

            ];
            $isSQL = $page -> add($data,[],true);
            if(!$isSQL){
                $this -> ajaxReturn(['status' => 1,'msg'=>'失败！']);
            }
            $this -> ajaxReturn(['status'=>0,'msg'=>'成功！']);
        }
    }
}